<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
	<div class="page-header">
		<h1><?=$mailInfo ? '编辑内部邮件' : '发布内部邮件'?></h1>
	</div>

	<div class="col-xs-12">
		<form class="form-horizontal" id="mail-form">
			<input type="hidden" name="id" value="<?=$mailInfo['id']?>" />
			<input type="hidden" name="mail_to_user" value="<?=$mailInfo['mail_to_user']?>" />
			<input type="hidden" name="mail_attachment" value="" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">邮件标题：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="mail_title" value="<?=$mailInfo['mail_title']?>" placeholder="请填写邮件标题"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">收件人：</label>
				<div class="col-sm-6">
					<span class="block input-icon input-icon-right pd0">
						<input class="width-100" type="text" placeholder="请选择教师" id="select-master" value="<?=$mailInfo['toUserName']?>" />
						<i class="ace-icon fa fa-user"></i>
					</span>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">邮件附件：</label>
				<div class="col-sm-6">
					<div class="form-control no-border">
						<a href="javascript:;" class="" id="upload-file"><i class="fa fa-file"></i> 上传附件</a>
					</div>
					<ul class="attachment-list pull-left list-unstyled" id="file-div">

					</ul>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">邮件内容：</label>
				<div class="col-sm-6">
					<textarea class="pd0" id="content" name="mail_content" style="height: 400px;width: 100%"><?=urldecode($mailInfo['mail_content'])?></textarea>
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<script type="text/javascript">
	$(function(){
		// 初始化编辑器
		var ue = UE.getEditor('content');
		// 上传对象
		var uploadTarget = $("#upload-file");

		// 初始化选择老师控件
		$("#select-master").selectTeacher({
			"multiSelect": true,
			"resultDom": $("[name=mail_to_user]"),
		});

		// 上传附件
		uploadTarget.uploader({
			"server":  '<?=UP_FORM_URL?>',
			"authorization": "<?=$signature?>",
			"policy": "<?=$policy?>",
			"accept": "file",
			"callback": function(response) {
				// 添加附件
				var fileName = decodeURIComponent(response.name);
				var fileType = response.type;
				var fileLink = upyunUrl + response.url;
				add_attachment(fileName, fileType, fileLink);
				// 添加附件
				save_attachment();
			}
		});
		// 删除附件
		$("body").on("click", ".attachment-file a.remove-file", function(){
			var me = $(this);
			layer.confirm('确定删除该附件吗？', function(){
				me.closest(".attachment-file").remove();
				layer.closeAll();
				// 重新保存附件
				save_attachment();
			})
		})

		// 编辑邮件
		<?php if($mailInfo): ?>
			// 邮件附件
			var attachment = <?=$mailInfo['mail_attachment'] ? $mailInfo['mail_attachment'] : "''"?>;
			for(var i in attachment){
				var fileName = attachment[i].name;
				var fileType = attachment[i].type;
				var fileLink = attachment[i].link;
				add_attachment(fileName, fileType, fileLink);
			}
			save_attachment();
		<?php endif; ?>

		// 提交表单
		var form = $("#mail-form");
		var submitUrl = "<?=admin_url('mail/save_mail_action')?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) {
					window.location.href = "<?=admin_url('mail/send')?>";
				}
			});
		})
	})

	// 添加附件
	function add_attachment(fileName, fileType, fileLink){
		$("#file-div").append('<li class="attachment-file" data-name="'+ fileName +'" data-type="'+ fileType +'" data-link="'+ fileLink +'">\
			<a href="javascript:;" class="attached-file">\
				<i class="ace-icon fa fa-film bigger-110"></i>\
				<span class="attached-name">'+ fileName +'</span>\
			</a>\
			<span class="action-buttons" style="position:relative; top:3px;">\
				<a href="'+ fileLink +'" target="_blank">\
					<i class="ace-icon fa fa-download bigger-125 blue"></i>\
				</a>\
				<a href="javascript:;" class="remove-file">\
					<i class="ace-icon fa fa-trash-o bigger-125 red"></i>\
				</a>\
			</span>\
		</li>')
	}

	// 保存附件
	function save_attachment(){
		var attachmentArray = new Array();
		$(".attachment-file").each(function(){
			var file = {
				"name": $(this).data("name"),
				"type": $(this).data("type"),
				"link": $(this).data("link")
			};
			attachmentArray.push(file);
		})
		console.log(attachmentArray);
		$("[name=mail_attachment]").val(JSON.stringify(attachmentArray));
	}
</script>